package com.atguigu.gmall.pms.controller;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import com.atguigu.gmall.pms.vo.SaleAttrValueVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gmall.pms.entity.SkuAttrValueEntity;
import com.atguigu.gmall.pms.service.SkuAttrValueService;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.common.bean.PageParamVo;

/**
 * sku销售属性&值
 *
 * @author AriceMoom
 * @email AriceMoom@atguigu.com
 * @date 2021-09-28 20:35:30
 */
@Api(tags = "sku销售属性&值 管理")
@RestController
@RequestMapping("pms/skuattrvalue")
public class SkuAttrValueController {

    @Autowired
    private SkuAttrValueService skuAttrValueService;

    @GetMapping("mapping/{spuId}")
    public ResponseVo<String> queryMappingBySpuId(@PathVariable("spuId")Long spuId){
        String json = this.skuAttrValueService.queryMappingBySpuId(spuId);
        return ResponseVo.ok(json);
    }

    @GetMapping("sku/{skuId}")
    public ResponseVo<List<SkuAttrValueEntity>> querySaleAttrValuesBySkuId(@PathVariable("skuId")Long skuId){
        List<SkuAttrValueEntity> skuAttrValueEntities = this.skuAttrValueService.list(new QueryWrapper<SkuAttrValueEntity>().eq("sku_id", skuId));
        return ResponseVo.ok(skuAttrValueEntities);
    }

    @GetMapping("spu/{spuId}")
    public ResponseVo<List<SaleAttrValueVo>> querySaleAttrValuesBySpuId(@PathVariable("spuId")Long spuId){
        List<SaleAttrValueVo> saleAttrValueVos = this.skuAttrValueService.querySaleAttrValuesBySpuId(spuId);
        return ResponseVo.ok(saleAttrValueVos);
    }

    @GetMapping("category/{cid}")
    public ResponseVo<List<SkuAttrValueEntity>> querySearchAttrValuesByCidAndSkuId(
            @PathVariable("cid")Long cid,
            @RequestParam("skuId")Long skuId
    ){
        List<SkuAttrValueEntity> skuAttrValueEntities = this.skuAttrValueService.querySearchAttrValuesByCidAndSkuId(cid, skuId);
        return ResponseVo.ok(skuAttrValueEntities);
    }

    /**
     * 列表
     */
    @GetMapping
    @ApiOperation("分页查询")
    public ResponseVo<PageResultVo> querySkuAttrValueByPage(PageParamVo paramVo){
        PageResultVo pageResultVo = skuAttrValueService.queryPage(paramVo);

        return ResponseVo.ok(pageResultVo);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("详情查询")
    public ResponseVo<SkuAttrValueEntity> querySkuAttrValueById(@PathVariable("id") Long id){
        SkuAttrValueEntity skuAttrValue = skuAttrValueService.getById(id);

        return ResponseVo.ok(skuAttrValue);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation("保存")
    public ResponseVo<Object> save(@RequestBody SkuAttrValueEntity skuAttrValue){
        skuAttrValueService.save(skuAttrValue);

        return ResponseVo.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseVo update(@RequestBody SkuAttrValueEntity skuAttrValue){
        skuAttrValueService.updateById(skuAttrValue);

        return ResponseVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseVo delete(@RequestBody List<Long> ids){
        skuAttrValueService.removeByIds(ids);

        return ResponseVo.ok();
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {

//        CompletableFuture.runAsync(() -> {
//            System.out.println("这是通过CompletableFuture.runAsync方法初始化一个异步任务。。" + Thread.currentThread().getName());
//        });
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("这是通过CompletableFuture.supplyAsync方法初始化一个异步任务。。" + Thread.currentThread().getName());
            //int i = 1/0;
            return "hello CompletableFuture supplyAsync";
        });
        CompletableFuture<String> future1 = future.thenApplyAsync(t -> {
            System.out.println("=================thenApplyAsync 1=====================");
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("上一个任务的返回结果1：" + t);
            return "hello thenApplyAsync";
        });
        CompletableFuture<Void> future2 = future.thenAcceptAsync(t -> {
            System.out.println("=================thenAcceptAsync= 2====================");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("上一个任务的返回结果2：" + t);
        });
        CompletableFuture<Void> future3 = future.thenRunAsync(() -> {
            System.out.println("=================thenRunAsync==== 3=================");
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("既不获取上一个任务的结果，也没有自己的返回结果");
        });

        CompletableFuture.anyOf(future1, future2, future3).join();

//                .whenCompleteAsync((t, u) -> {
//            System.out.println("========================whenCompleteAsync==============================");
//            System.out.println("正常执行的返回结果集t: " + t);
//            System.out.println("出现异常的异常信息u: " + u);
//        }).exceptionally(t -> {
//            System.out.println("============================exceptionally================================");
//            System.out.println("异常信息t: " + t);
//            return "hello exceptionnally";
//        });
        //System.out.println(future.get());

        System.out.println("这是main方法：" + Thread.currentThread().getName());

        System.in.read();

        //new MyThread().start();
//        new Thread(() -> {
//            System.out.println("实现Runnable接口的方式初始化了一个多线程程序" + Thread.currentThread().getName());
//        }).start();

//        FutureTask<String> futureTask = new FutureTask<>(() -> {
//            System.out.println("1实现Callable接口的方式初始化了一个多线程程序" + Thread.currentThread().getName());
//
//            return "hello Callable!";
//        });
//        new Thread(futureTask).start();
//
//        while (!futureTask.isDone()) {
//            System.out.println("异步任务没有执行完成");
//        }
//        System.out.println("异步任务已完成");

//        try {
//            System.out.println(futureTask.get() + "2");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

//        ExecutorService executorService = new ThreadPoolExecutor(3, 5,
//                60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10), Executors.defaultThreadFactory());
//
//        Future<String> future = executorService.submit(() -> {
//            System.out.println("这是线程池的方式提交任务");
//            return "hello Executor";
//        });
//
//        try {
//            System.out.println(future.get());
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
//
//        System.out.println("3我是主线程" + Thread.currentThread().getName());
    }

}

class MyThread extends Thread{

    @Override
    public void run() {
        System.out.println("继承Thread基类的方式初始化了一个多线程程序" + Thread.currentThread().getName());
    }
}
